using LitJson;
namespace CGF
{
    public partial class PanelRegister : UIComponentBase, IAwake<UILayer, bool>, IDestroy
    {
        public static void Open()
        {
            UIManagerComponent.instance.OpenUI<PanelRegister>(UILayer.Normal, RootComponent.instance, false);
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<PanelRegister>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/GameUI/PanelRegister", layer, isFloat);
            BindProperties(transform);

            BtnBack.SetClick(Close);

            BtnSubmit.SetClick(async () =>
            {
                if (InputUsername.text.Contains(" "))
                {
                    Dialog.Alert(CommAssets.instance.I18N_account_canot_space);
                    return;
                }
                if (InputUsername.text.Length < 5)
                {
                    Dialog.Alert(CommAssets.instance.I18N_username_length_invalid.Replace("{num}", "5"));
                    return;
                }
                if (!StringHelper.IsNumberAndLetter(InputPassword.text))
                {
                    Dialog.Alert(I18N_invalid_passwd);
                    return;
                }
                if (InputPassword.text != InputPasswordConfirm.text)
                {
                    Dialog.Alert(I18N_two_passwd_not_same);
                    return;
                }
                JsonData data = new JsonData();
                data["user_name"] = InputUsername.text;
                data["password"] = InputPassword.text;
                data["invite_code"] = InputInviteCode.text;
                Network.Res res = await Network.Post("/v1/oauth/register", data);
                if (res.success)
                {
                    Dialog.Alert(I18N_register_success, Close);
                }
            });
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
        }

    }
}
